package com.itheima.zhuanhuan;

import com.itheima.domain.Student;
import com.itheima.exception.AgeOutOfBoundsException;

import java.util.Scanner;

/*
键盘录入学生的姓名和年龄,其中年龄为18 - 25岁,
超出这个范围是异常数据不能赋值.需要重新录入,一直录到正确为止
 */
public class Test02 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        Student student = null;
        while (true) {
            System.out.println("请输入年龄");
            String ageStr = sc.nextLine();
            try {
                int age = Integer.parseInt(ageStr);//NumberFormatException
                student = new Student();
                student.setName(name);

                student.setAge(age);
                break;
            } catch (NumberFormatException e) {
                System.out.println("请输入数字作为年龄");

            } catch (AgeOutOfBoundsException e){
                e.printStackTrace();
                System.out.println("请输入18-25的年龄");
            }

        }


        System.out.println(student);
    }


}
